package com.parking.order.consumer;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.parking.order.dao.OrderInfoDao;
import com.parking.order.dao.OrderLogsDao;
import com.parking.order.model.OrderInfo;
import com.parking.order.model.OrderLogs;
import com.parking.order.model.exception.OrderException;
import com.parking.order.model.exception.OrderExceptionCode;
import com.parking.order.service.IOrderLogsService;
import org.springframework.amqp.rabbit.annotation.Queue;
import org.springframework.amqp.rabbit.annotation.RabbitHandler;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * 订单日志消费者类
 * */
@Component
public class OrderLogConsumer {
    /**
     * 订单日志数据层
     * */
    @Autowired
    private OrderLogsDao orderLogsDao;

    /**
     * 订单信息数据层
     * */
    @Autowired
    private OrderInfoDao orderInfoDao;

    /**
     * 订单信息服务层
     * */
    @Autowired
    private IOrderLogsService orderLogsService;

    /**
     * 生成订单日志
     * 参数 data 是四个数据的拼接
     *     orderInfoNum      订单编号
     *     orderLogsDescribe 日志描述
     *     logsPersonType    日志操作人类型
     *     logsUpdatePerson  日志操作人
     * */
    @RabbitHandler
    @RabbitListener(queuesToDeclare = {@Queue(value="orderLog")})
    public void orderLogConsume(String data) {
        System.out.println("==================添加订单日志============================");
        String[] infos = data.split(",");
        String orderInfoNum = infos[0];
        String orderLogsDescribe = infos[1];
        Integer logsPersonType = Integer.valueOf(infos[2]);
        String logsUpdatePerson = infos[3];

        OrderInfo order = orderInfoDao.selectOne(new QueryWrapper<OrderInfo>().eq("order_info_num", orderInfoNum));
        if(order==null) throw new OrderException(OrderExceptionCode.ORDER_EXCEPTION_CODE_ORDER_NOT_EXISTS);
        OrderLogs orderLogs = new OrderLogs(null,order.getOrderInfoId(),orderInfoNum,orderLogsDescribe, LocalDateTime.now(),logsUpdatePerson,logsPersonType);
        orderLogs.setCertifiedInfoId(order.getCertifiedInfoId());
        orderLogsDao.insert(orderLogs);
        System.out.println("订单日志Id:"+orderLogs.getOrderLogsId());
        System.out.println("订单Id:"+orderLogs.getOrderInfoId());
        System.out.println("订单编号:"+orderLogs.getOrderInfoNum());
        System.out.println("日志描述:"+orderLogs.getOrderLogsDescribe());
        System.out.println("更新时间:"+orderLogs.getLogsUpdateTime());
        System.out.println("操作人:"+orderLogs.getLogsUpdatePerson());
        System.out.println("操作人类型:"+orderLogs.getLogsPersonType()+"(0系统自动 1用户 2物业)");
        System.out.println("物业信息Id:"+orderLogs.getCertifiedInfoId());
    }

    /**
     * 批量生成订单日志
     * 参数 data 是若干数据的拼接
     *     orderIdsStr       订单信息Id
     *     orderLogsDescribe 日志描述
     *     logsPersonType    日志操作人类型
     *     logsUpdatePerson  日志操作人
     * */
    @RabbitHandler
    @RabbitListener(queuesToDeclare = {@Queue(value="orderLogBatch")})
    public void orderLogBatchConsume(String data) {
        System.out.println("=======================批量添加订单日志========================");
        String[] infos = data.split(",");
        int n = infos.length;
        String orderLogsDescribe = infos[n-3];
        Integer logsPersonType = Integer.valueOf(infos[n-2]);
        String logsUpdatePerson = infos[n-1];

        List<Integer> orderIds = new ArrayList<>();
        for(int i=0;i<n-3;i++){
            orderIds.add(Integer.valueOf(infos[i]));
        }
        List<OrderInfo> orders = orderInfoDao.selectBatchIds(orderIds);

        LocalDateTime now = LocalDateTime.now();

        System.out.println("日志描述:"+orderLogsDescribe);
        System.out.println("更新时间:"+now);
        System.out.println("操作人:"+logsUpdatePerson);
        System.out.println("操作人类型:"+logsPersonType+"(0系统自动 1用户 2物业)");

        List<OrderLogs> orderLogs = new ArrayList<>();
        for (OrderInfo order : orders) {
            OrderLogs orderLog = new OrderLogs(null,order.getOrderInfoId(),order.getOrderInfoNum(),orderLogsDescribe, now,logsUpdatePerson,logsPersonType);
            orderLog.setCertifiedInfoId(order.getCertifiedInfoId());
            orderLogs.add(orderLog);
        }
        orderLogsService.saveBatch(orderLogs);

        for (OrderLogs orderLog : orderLogs) {
            System.out.println("===================================");
            System.out.println("日志Id:"+orderLog.getOrderLogsId());
            System.out.println("订单Id:"+orderLog.getOrderInfoId());
            System.out.println("订单编号:"+orderLog.getOrderInfoNum());
            System.out.println("物业信息Id:"+orderLog.getCertifiedInfoId());
            System.out.println("===================================");
        }
    }
}
